<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">
    <el-form-item label="客户" prop="customerId">
      <el-select v-model="dataForm.customerId" placeholder="请选择">
        <el-option @click.native="getCustomer(dataForm.customerId)"
          v-for="item in optionsCustomer"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="商品批次号" prop="commodityBatch" >
      <el-select multiple v-model="dataForm.commodityBatch" placeholder="请选择"
                 @remove-tag="deleteTag(dataForm.commodityBatch)"
                 @change="getBatch">
        <el-option
                   v-for="item in optionsBatch"
                   :key="item.id"
                   :label="item.commodityBatch"
                   :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <div v-for="(item, index) in commodityBatchList" :key="index" v-model="commodityBatchVaile">
      <el-form-item :label="item.commodityName+'面积'">
        <el-input-number v-model="dataForm.area[index]" placeholder="面积"></el-input-number>
      </el-form-item>
      <el-form-item :label="item.commodityName+'出库数量'">
        <el-input-number v-model="dataForm.commodityNum[index]" placeholder="出库数量"></el-input-number>
      </el-form-item>
    </div>


    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        optionsCustomer: [],
        optionsBatch: [],
        commodityBatchList: [],
        commodityBatchVaile: false,
        area: [],
        commodityNum: [],
        dataForm: {
          id: 0,
          entId: '',
          area: [],
          commodityNum: [],
          applicationNum: '',
          auditingStatus: '',
          auditingRemarks: '',
          status: '',
          customerId: '',
          storageId: '',
          storageName: '',
          spaceId: '',
          spaceName: '',
          commodityBatch: [],
        },
        dataRule: {
          customerId: [
            { required: true, message: '客户不能为空', trigger: 'blur' }
          ],
          commodityBatch: [
            { required: true, message: '商品批次号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      deleteTag (data) {
        if (data.length === 0) {
          this.commodityBatchList = []
        }
        for (let i = 0; i < this.commodityBatchList.length; i++) {
          if (this.commodityBatchList[i].id = data) {
            this.commodityBatchList.splice(i, i)
          }
        }
      },
      getBatch (data) {
        this.commodityBatchList = []
        if (data.length !== 0) {
          this.$http({
            url: this.$http.adornUrl(`/app/entinventory/infos/${data}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.commodityBatchVaile = true
              for (let i = 0; i < data.entInventory.length; i++) {
                this.commodityBatchList.push(data.entInventory[i])
              }
            } else {
              this.commodityBatchList = []
            }
          })
        } else {
          this.commodityBatchList = []
        }
      },
      getCustomer (data) {
        this.$http({
          url: this.$http.adornUrl(`/app/entinventory/query/${data}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.optionsBatch = data.list
          } else {
            this.optionsBatch = []
          }
        })
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/entoutbound/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.customerId = data.entOutbound.customerId
                this.dataForm.id = data.entOutbound.id
                this.getCustomer(data.entOutbound.customerId)
                this.dataForm.commodityBatch.push(data.entOutbound.commodityBatch)
                for (let i = 0; i < data.entOutbound.list.length; i++) {
                  this.commodityBatchList.push(data.entOutbound.list[i])
                  this.dataForm.area.push(data.entOutbound.list[i].commodityArea)
                  this.dataForm.commodityNum.push(data.entOutbound.list[i].commodityNum)
                }
              }
            })
          }
        })
        // 客户接口
        this.$http({
          url: this.$http.adornUrl(`/app/entcustomer/listCustomer`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.optionsCustomer = data.list
          } else {
            this.optionsCustomer = []
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/entoutbound/${!this.dataForm.id ? 'saveOutbound' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'customerId': this.dataForm.customerId,
                'commodityNum': this.dataForm.commodityNum,
                'area': this.dataForm.area,
                'commodityBatch': this.dataForm.commodityBatch
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
